package com.coolgor.axnut.service.mdmis.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.mdmis.TaskTraceDao;
import com.coolgor.axnut.dto.mdmis.TaskTraceExecution;
import com.coolgor.axnut.entity.mdmis.TaskTrace;
import com.coolgor.axnut.entity.mdmis.TaskTraceCondition;
import com.coolgor.axnut.enums.mdmis.TaskTraceOperationStateEnum;
import com.coolgor.axnut.exceptions.mdmis.TaskTraceOperationException;
import com.coolgor.axnut.service.mdmis.TaskTraceService;
import com.coolgor.axnut.util.PageCalculator;



@Service
public class TaskTraceServiceImpl implements TaskTraceService {
	private static Logger log = LoggerFactory.getLogger(TaskTraceServiceImpl.class);
	
	@Autowired
	private TaskTraceDao taskTraceDao;
	

	@Override
	public TaskTraceExecution getTaskTraceList(TaskTraceCondition taskTraceCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<TaskTrace> taskTraceList = taskTraceDao.queryTaskTraceList(taskTraceCondition, rowIndex, pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = taskTraceDao.queryTaskTraceCount(taskTraceCondition);
		TaskTraceExecution result = new TaskTraceExecution();
		if (taskTraceList != null) {
			result.setState(TaskTraceOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(TaskTraceOperationStateEnum.SUCCESS.getStateInfo());
			result.setTaskTraceList(taskTraceList);
//			result.setTaskTrace(taskTraceList.get(0));
			result.setCount(count);
		} else {
			result.setState(TaskTraceOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public TaskTraceExecution addTaskTrace(TaskTrace taskTrace) throws TaskTraceOperationException {
		// 空值判断
		if (taskTrace == null) {
			return new TaskTraceExecution(TaskTraceOperationStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 添加信息
			effectedNum = taskTraceDao.insertTaskTrace(taskTrace);
			if (effectedNum <= 0) {
				throw new TaskTraceOperationException("插入配送系统自动执行信息记录失败");
			} 
		} catch (Exception e) {
			throw new TaskTraceOperationException("Add TaskTrace error:" + e.getMessage());
		}
		return new TaskTraceExecution(TaskTraceOperationStateEnum.SUCCESS, taskTrace);
	}

	@Override
	@Transactional
	public TaskTraceExecution modifyTaskTrace(TaskTrace taskTrace) throws TaskTraceOperationException {
		// 空值判断
		if (taskTrace == null) {
			return new TaskTraceExecution(TaskTraceOperationStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 更新操作
			effectedNum = taskTraceDao.updateTaskTrace(taskTrace);
			if (effectedNum <= 0) {
				throw new TaskTraceOperationException("配送系统自动执行信息更新失败");
			} 
		} catch (Exception e) {
			throw new TaskTraceOperationException("Modify TaskTrace error:" + e.getMessage());
		}
		return new TaskTraceExecution(TaskTraceOperationStateEnum.SUCCESS, taskTrace);
	}

	@Override
	@Transactional
	public TaskTraceExecution removeTaskTrace(TaskTrace taskTrace) throws TaskTraceOperationException {
		// 空值判断
		if (taskTrace == null) {
			return new TaskTraceExecution(TaskTraceOperationStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = taskTraceDao.deleteTaskTrace(taskTrace);
			if (effectedNum <= 0) {
				throw new TaskTraceOperationException("配送系统自动执行信息删除失败");
			} 
		} catch (Exception e) {
			throw new TaskTraceOperationException("Delete MDMIS Task Trace error:" + e.getMessage());
		}
		return new TaskTraceExecution(TaskTraceOperationStateEnum.SUCCESS, taskTrace);
	}

	@Override
	@Transactional
	public TaskTraceExecution batchRemoveTaskTrace(List<TaskTrace> taskTraceList) throws TaskTraceOperationException {
		// 空值判断
		if (taskTraceList == null) {
			return new TaskTraceExecution(TaskTraceOperationStateEnum.INNER_ERROR);
		}
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<taskTraceList.size(); i++) {
			recordIDList.add(taskTraceList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = taskTraceDao.batchDeleteTaskTrace(recordIDList);
			if (effectedNum <= 0) {
				throw new TaskTraceOperationException("配送系统自动执行信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new TaskTraceOperationException("Batch Delete MDMIS Task Trace error:" + e.getMessage());
		}
		TaskTraceExecution exe = new TaskTraceExecution(TaskTraceOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

}
